﻿using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.Mvc;

namespace Nasa8x.Mvc.Helpers
{
    public static class TempDataExtensions
    {
        public static void PopulateFrom(this TempDataDictionary tempData, object o)
        {
            foreach (PropertyValue property in o.GetProperties())
            {
                tempData[property.Name] = property.Value;
            }
        }

        public static void PopulateFrom(this TempDataDictionary tempData
          , NameValueCollection nameValueCollection)
        {
            foreach (string key in nameValueCollection.Keys)
                tempData[key] = nameValueCollection[key];
        }

        public static void PopulateFrom(this TempDataDictionary tempData
          , IDictionary<string, object> dictionary)
        {
            foreach (string key in dictionary.Keys)
                tempData[key] = dictionary[key];
        }

        public static string SafeGet(this TempDataDictionary tempData, string key)
        {
            object value;
            if (!tempData.TryGetValue(key, out value))
                return string.Empty;
            return value.ToString();
        }
    }
}
